using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Diagnostics.Contracts;
using Konst3d.Efficiency.Suite;

namespace PEM
{
    internal partial class CountDownTimersViewModel : ViewModelBase
    {
        #region Types

        internal delegate void EventHandler(CountDownTimersViewModel sender);

        #endregion // Types

        #region Events

        public event EventHandler SuiteOpened;
        public event EventHandler SuiteClosed;
        public event EventHandler SuiteCreated;
        public event EventHandler SuiteDeleted;
        public event EventHandler TimerSelected;

        #endregion // Events

        #region Properties

        public IEnumerable<ICountDownTimer> Timers
        {
            get
            {
                return _parentModel.Suite != null ? _parentModel.Suite.CountDownTimers : null;
            }
        }

        public ICountDownTimer SelectedTimer
        {
            get
            {
                return _selectedTimer;
            }
            set
            {
                _selectedTimer = value;

                if (TimerSelected != null)
                {
                    TimerSelected(this);
                }

				NotifyPropertyChanged("SelectedTimer");
            }
        }

        public CommandsCollection Commands
        {
            get;
            private set;
        }

        public IEfficiencySuite Suite
        {
            get
            {
                return _parentModel.Suite;
            }
        }

        #endregion // Properties

        #region Constructor

        public CountDownTimersViewModel(Window parentWindow, MainWindowViewModel parentModel)
        {
            Contract.Requires(parentWindow != null);
            Contract.Requires(parentModel != null);

            _parentWindow = parentWindow;
            _parentModel = parentModel;

            _parentModel.SuiteOpened += OnSuiteOpened;
            _parentModel.SuiteClosed += OnSuiteClosed;
            _parentModel.SuiteCreated += OnSuiteCreated;
            _parentModel.SuiteDeleted += OnSuiteDeleted;

            _createEditCountDownDlg = new CreateCountDownDialog();

            Commands = new CommandsCollection(this);
        }

        #endregion // Constructor

        #region Overriden Methods

        protected override void DisposeManaged()
        {
            base.DisposeManaged();

            _createEditCountDownDlg.Close();
            _createEditCountDownDlg = null;

            _parentModel.SuiteOpened -= OnSuiteOpened;
            _parentModel.SuiteClosed -= OnSuiteClosed;
            _parentModel.SuiteCreated -= OnSuiteCreated;
            _parentModel.SuiteDeleted -= OnSuiteDeleted;
            _parentModel = null;
        }

        #endregion // Overriden Methods

        #region Event Handlers

        private void OnSuiteOpened(MainWindowViewModel sender)
        {
            NotifyPropertyChanged("Timers");

            if (SuiteOpened != null)
            {
                SuiteOpened(this);
            }
        }

        private void OnSuiteClosed(MainWindowViewModel sender)
        {
            NotifyPropertyChanged("Timers");

            if (SuiteClosed != null)
            {
                SuiteClosed(this);
            }
        }

        private void OnSuiteCreated(MainWindowViewModel sender)
        {
            NotifyPropertyChanged("Timers");

            if (SuiteCreated != null)
            {
                SuiteCreated(this);
            }
        }

        private void OnSuiteDeleted(MainWindowViewModel sender)
        {
            NotifyPropertyChanged("Timers");

            if (SuiteDeleted != null)
            {
                SuiteDeleted(this);
            }
        }

        #endregion // Event Handlers

        #region Fields

        private Window _parentWindow;
        private MainWindowViewModel _parentModel;
        private CreateCountDownDialog _createEditCountDownDlg;
        private ICountDownTimer _selectedTimer;

        #endregion // Fields
    }
}
